var unindent = require('../utils/unindent');

function parse(content) {
    content = content.trim();

    // replace Linebreak with Unicode
    content = content.replace(/\n/g, '\uffff');

    // 2021-02-20 {fix} 修复xx问题
    var parseRegExp = /^(.+?)(?:\s+(?:(?:\{(.+?)\})?\s*)?(.+?))?$/g;
    var matches = parseRegExp.exec(content);

    if (!matches)
        return null;

    // reverse Unicode Linebreaks
    matches.forEach(function (val, index, array) {
        if (val) {
            array[index] = val.replace(/\uffff/g, '\n');
        }
    });

    const desc = unindent(matches[3] || '');

    const list = desc.split(' ');

    const version = list.shift();
    const description = list.length ? list.join(' ') : '';

    return {
        date: matches[1],
        type: matches[2] || '',
        version: version,
        description: description
    };
}


/**
 * Exports
 */
module.exports = {
    parse: parse,
    method: 'push',
    markdownFields: ['description']
};
